﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EQT.Dal;
using System.Threading;
using EQT.Model;

public partial class SampleKept : System.Web.UI.Page
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private DaSampleKept dalKept = new DaSampleKept();
    private DaTfile dalTfile = new DaTfile();

    private UserContext context;

    protected void Page_Load(object sender, EventArgs e)
    {
        context = Session["USER_CONTEXT"] as UserContext;
        OperationResult.Text = "";
        divResult.Visible = false;
        if (!IsPostBack)
        {
            AspNetPager.PageSize = 15;

            this.BtnQuery_Click(null, null);

            this.SetPanelVisiable(SummaryPanel);

            if (Request.QueryString["sampleid"] != null && !string.IsNullOrEmpty(Request.QueryString["sampleid"].ToString()))
            {
                this.DisplayEditInfo(Request.QueryString["sampleid"].ToString());
                this.SetPanelVisiable(EditPanel);
            }
        }
    }

    protected void EditSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoSampleKept mod = new MoSampleKept();
        int count = dalKept.GetCount(" where Sample_Name='" + EditSample_Name.Text.Trim() + "'");

        if (count > 0)
        {
            OperationResult.Text = "样品名称已经存在,请重新输入！"; divResult.Visible = true;
            return;
        }

        try
        {
            mod.Sam_Kep_Id = EditSK_ID.Text;
            mod.Receiver = EditReceiver.Text.Trim();
            mod.Sender = EditSender.Text.Trim();
            mod.Sender_Tel = EditSender_Tel.Text.Trim();
            mod.Sender_Corp = EditSender_Corp.Text.Trim();
            mod.Sample_Name = EditSample_Name.Text.Trim();
            mod.Model = EditModel.Text.Trim();
            mod.Qty = int.Parse(EditQTY.Text.Trim());
            mod.Sample_Status = "良好";
            mod.Receive_Date = DateTime.Now;
            mod.Store_Addr = EditStore_Addr.Text.Trim();
            mod.Rem = EditRem.Text.Trim();
            //mod.SampleSequence = EditSample_Name.Text.Trim();

            int rtn = dalKept.Update(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "样品登记信息修改成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "样品登记信息修改失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "样品登记信息修改失败！"; divResult.Visible = true;
        }
    }

    protected void EditBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateSave_Click(object sender, EventArgs e)
    {
        //执行保存操作。
        MoSampleKept mod = new MoSampleKept();

        int count = dalKept.GetCount(" where Sample_Name='" + CreateSample_Name.Text.Trim() + "'");

        if (count > 0)
        {
            OperationResult.Text = "样品名称已经存在,请重新输入！"; divResult.Visible = true;
            return;
        }
        try
        {
            mod.Sam_Kep_Id = SequenceKeys.GenerateSampleKeptID();
            mod.Receiver = CreateReceiver.Text.Trim();
            mod.Sender = CreateSender.Text.Trim();
            mod.Sender_Tel = CreateSender_Tel.Text.Trim();
            mod.Sender_Corp = CreateSender_Corp.Text.Trim();
            mod.Sample_Name = CreateSample_Name.Text.Trim();
            mod.Model = CreateModel.Text.Trim();
            mod.Qty = int.Parse(CreateQTY.Text.Trim());
            mod.Sample_Status = "良好";
            mod.Receive_Date = DateTime.Now;
            mod.Store_Addr = CreateStore_Addr.Text.Trim();
            mod.Rem = CreateRem.Text.Trim();
            //mod.SampleSequence = CreateSample_Name.Text.Trim();

            int rtn = dalKept.Add(mod);

            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "样品登记信息保存成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "样品登记信息保存失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "样品登记信息保存失败！"; divResult.Visible = true;
        }
    }

    protected void CreateBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void CreateGo_Click(object sender, EventArgs e)
    {
        this.CreateCancel();
        this.SetPanelVisiable(CreatePanel);
    }

    protected void DetailBack_Click(object sender, EventArgs e)
    {
        this.SetPanelVisiable(SummaryPanel);
    }

    protected void EditGo_Click(object sender, EventArgs e)
    {
        string strKey = DetailSK_ID.Text.Trim();
        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void DeleteGo_Click(object sender, EventArgs e)
    {
        try
        {
            string strKey = DetailSK_ID.Text.Trim();
            int rtn = this.DeleteInfo(strKey);
            this.BtnQuery_Click(null, null);
            if (rtn == 1)
            {
                this.BtnQuery_Click(null, null);
                OperationResult.Text = "样品登记信息删除成功！"; divResult.Visible = true;
            }
            else
            {
                OperationResult.Text = "样品登记信息删除失败！"; divResult.Visible = true;
            }
        }
        catch (Exception ex)
        {
            Utility.GlobalLog.Error(ex);
            OperationResult.Text = "样品登记信息删除失败！"; divResult.Visible = true;
        }
        //这里要给用户一个删除结果提示。
    }

    protected void GvDelete_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DeleteInfo(strKey);
        this.BtnQuery_Click(null, null);
    }

    protected void GvExchange_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        Response.Redirect("SampleKeptLog.aspx?samplekeptid='"+strKey+"'");
    }

    protected void GvEdit_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayEditInfo(strKey);
        this.SetPanelVisiable(EditPanel);
    }

    protected void GvCancel_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.CancelRegister(strKey);
        this.BtnQuery_Click(null, null);
    }

    protected void GvDetail_Click(object sender, EventArgs e)
    {
        LinkButton btn = sender as LinkButton;
        GridViewRow gvr = (GridViewRow)btn.NamingContainer;
        string strKey = gvr.Cells[0].Text.Trim();

        this.DisplayDetailInfo(strKey);
        this.SetPanelVisiable(DetailPanel);
    }

    protected int CancelRegister(string strKey)
    {
        return dalKept.CancelRegister(strKey);
    }

    protected void SetPanelVisiable(Panel panel)
    {
        EditPanel.Visible = false;
        DetailPanel.Visible = false;
        CreatePanel.Visible = false;
        SummaryPanel.Visible = false;

        panel.Visible = true;
    }

    protected void BtnQuery_Click(object sender, EventArgs e)
    {
        ////这里查询员工信息并绑定到gridview上。
        string strCondition;

        if (cbStatus.Checked)
        {
            strCondition = "where 1=1 ";
        }
        else
        {
            strCondition = "where Sample_Status<>'注销' ";
        }

        int pagecount = 0;
        if (sender != null && e != null)
        {
            AspNetPager.CurrentPageIndex = 0;
        }
        if (!string.IsNullOrEmpty(TxtSample_ID.Text.Trim()))
        {
            strCondition = strCondition + "and  Sam_Kep_ID='" + TxtSample_ID.Text.Trim() + "'";
        }
        if (!string.IsNullOrEmpty(TxtSampleName.Text.Trim()))
        {
            strCondition = strCondition + "and  Sample_Name like '%" + TxtSampleName.Text.Trim() + "%'";
        }

        this.GridView.DataSource = dalKept.GetSampleKeptRecords(strCondition, AspNetPager.PageSize, AspNetPager.CurrentPageIndex - 1, out pagecount);
        GridView.DataBind();
        AspNetPager.RecordCount = pagecount;
        this.SetPanelVisiable(SummaryPanel);
    }

    protected int DeleteInfo(string strKey)
    {
        return dalKept.Delete(strKey);
    }

    protected void DisplayDetailInfo(string strKey)
    {
        DataSet ds = dalTfile.QueryFileRelation(strKey);
        RepFileDetail.DataSource = ds;
        RepFileDetail.DataBind();

        //这里查询员工信息
        MoSampleKept mod = dalKept.GetEntity(strKey);

        //然后给查看视图赋值。
        DetailSK_ID.Text = mod.Sam_Kep_Id;
        DetailReceiver.Text = mod.Receiver;
        DetailSender.Text = mod.Sender;
        DetailSender_Tel.Text = mod.Sender_Tel;
        DetailSender_Corp.Text = mod.Sender_Corp;
        DetailSample_Name.Text = mod.Sample_Name;
        DetailModel.Text = mod.Model;
        DetailQTY.Text = mod.Qty.ToString();
        DetailSample_Status.Text = mod.Sample_Status;
        DetailReceive_Date.Text = mod.Receive_Date.ToString("yyyy-MM-dd");
        DetailStore_Addr.Text = mod.Store_Addr;
        DetailRem.Text = mod.Rem;
    }

    protected void DisplayEditInfo(string strKey)
    {
        DataSet ds= dalTfile.QueryFileRelation(strKey);
        RepFileEidt.DataSource = ds;
        RepFileEidt.DataBind();
                            
        btnAddAttachment.NavigateUrl = "uploadfiles.aspx?sample&id="+strKey+"&m=0";
        //这里查询员工信息
        MoSampleKept mod = dalKept.GetEntity(strKey);

        //然后给编辑视图赋值。
        EditSK_ID.Text = mod.Sam_Kep_Id;
        EditReceiver.Text = mod.Receiver;
        EditSender.Text = mod.Sender;
        EditSender_Tel.Text = mod.Sender_Tel;
        EditSender_Corp.Text =mod.Sender_Corp;
        EditSample_Name.Text = mod.Sample_Name;
        EditModel.Text = mod.Model;
        EditQTY.Text = mod.Qty.ToString();
        //EditSample_Status.Text = mod.Sample_Status;
        //EditReceive_Date.Text = mod.Receive_Date.ToString("yyyy-MM-dd");
        EditStore_Addr.Text = mod.Store_Addr;
        EditRem.Text = mod.Rem;

    }

    protected void CreateCancel()
    {
        CreateSK_ID.Text = "";
        CreateReceiver.Text = "";
        CreateSender.Text = "";
        CreateSender_Tel.Text = "";
        CreateSender_Corp.Text = "";
        CreateSample_Name.Text = "";
        CreateModel.Text = "";
        CreateQTY.Text = "";
        //CreateSample_Status.Text = "";
        //CreateReceive_Date.Text = "";
        CreateStore_Addr.Text = "";
        CreateRem.Text = "";
    }

    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {
        this.BtnQuery_Click(null, null);
    }

    protected void RepFileEidt_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        string strKey = e.CommandName;
        try
        {
            dalTfile.DeleteOneAttachFile(strKey);
            this.DisplayEditInfo(EditSK_ID.Text.Trim());
            OperationResult.Text = "删除附件成功!"; divResult.Visible = true;
        }
        catch (Exception ex)
        {
            log.Error(ex);
            OperationResult.Text = "删除附件失败!"; divResult.Visible = true;
        }
    }
}
